package org.jeecg.modules.task.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.handle.ISendMsgHandle;
import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum;
import org.jeecg.modules.message.handle.enums.SendMsgTypeEnum;
import org.jeecg.modules.message.service.ISysMessageService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 任务定时发送
 */
@Slf4j
public class TaskPublishJob implements Job {

	@Autowired
	private IContentInfoService contentInfoService;

	@Autowired
	private IActivityInfoService activityInfoService;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info("测试定时"+ DateTimeUtil.getTodayChar12());
		QueryWrapper<ContentInfo> contentInfoQueryWrapper = new QueryWrapper<>();
		contentInfoQueryWrapper.eq("is_release","2").le("timed_release_time",DateUtils.formatDateTime());
		List<ContentInfo> contentInfoList = contentInfoService.list(contentInfoQueryWrapper);


		QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
		activityInfoQueryWrapper.eq("is_release","2").le("timed_release_time",DateUtils.formatDateTime());
		List<ActivityInfo> activityInfoList = activityInfoService.list(activityInfoQueryWrapper);


		if(null != contentInfoList && contentInfoList.size() >0){
			contentInfoList.stream().forEach(item->{
				UpdateWrapper<ContentInfo> updateWrapper = new UpdateWrapper<>();
				updateWrapper.set("is_release","1");
				updateWrapper.set("release_time",DateUtils.now());
				updateWrapper.eq("task_id",item.getTaskId());
				contentInfoService.update(updateWrapper);
			});
		}


		if(null != activityInfoList && activityInfoList.size() >0){
			activityInfoList.stream().forEach(item->{
				UpdateWrapper<ActivityInfo> updateWrapper = new UpdateWrapper<>();
				updateWrapper.set("is_release","1");
				updateWrapper.set("release_time",DateUtils.now());
				updateWrapper.eq("task_id",item.getTaskId());
				activityInfoService.update(updateWrapper);
			});
		}
	}

}
